﻿using System;
using System.Net;

using AnB.Common;
using AnB.Net;
using AnB.Proto;

namespace AnBApp
{
    class Program
    {
        static void TestConnection(Connection connection)
        {
            long ts = connection.Request<long>("System.Timestamp", 9832);
            Console.WriteLine(ts);
            string echo = connection.Request<string>("System.Echo", "Hello");
            Console.WriteLine(echo);
            var person = connection.Request<Person>("Player.EchoPerson", new Person()
            {
                Id = 9527,
                Name = "Hello",
            });
            Console.WriteLine("person {0}/{1}", person.Id, person.Name);
            //connection.RequestAsync<long>("System.Timestamp", 123);

            connection.RawSendLengthy(new byte[]
                {
                    1,
                    1,
                    1,
                    1,
                    0x23,
                    0,
                    1,
                    1,
                });
        }
        static void Test()
        {
            Log.Initialize("Log.xml");
            var dispatcher = new Dispatcher(typeof(TypeMethodCmdHandler), "AnB.App.Handler");
            dispatcher.Load(App.Config("cmds.xml"), new string[]
                {
                    "System",
                    "AnB.Proto",
                });
            //var server = new Server(typeof(Connection), Server.DEFAULT_BACKLOG, dispatcher);
            //server.Start(9986);
            var client = new Client(typeof(Connection), dispatcher);
            client.Start("localhost", 9986, (e, c) =>
                {
                    if (e != Error.Code.None)
                    {
                        // failed;
                        return;
                    }

                    try
                    {
                        TestConnection((Connection)c);
                    }
                    catch (Exception ex)
                    {
                        Log.Get().Error(ex);
                    }
                    //client.Stop();
                });
        }
        static void Main(string[] args)
        {
            long t = Utils.ParseBinary("8G10M12K88B123");
            Test();
            Console.ReadLine();
        }
    }
}
